<?php
$this->layout='//layouts/column1';
$this->breadcrumbs=array(
	'Loan Search',
);
?>

<h1>Loan Search</h1>
<?php
Yii::app()->clientScript->registerScript('search-by-organization', "
$('#advanced-search-button').click(function(){
	$('#advanced-search-options').toggle('slow');
	if($(this).hasClass('advanced-search-toggle-expanded')){
		$(this).removeClass('advanced-search-toggle-expanded')
	}else{
		$(this).addClass('advanced-search-toggle-expanded')
	}
	return false;
});
var reloadStatistic=function(){
	var loading='Loading...';
	$('#loan-result-statistic-amount').html(loading);
	$('#loan-result-statistic-amount-average').html(loading);
	$('#loan-result-statistic-amount-max').html(loading);
	$('#loan-result-statistic-amount-min').html(loading);
	$('#loan-result-statistic-rate-average').html(loading);
	$('#loan-result-statistic-rate-max').html(loading);
	$('#loan-result-statistic-rate-min').html(loading);
	$('#loan-result-statistic-pricing-average').html(loading);
	$('#loan-result-statistic-pricing-max').html(loading);
	$('#loan-result-statistic-pricing-min').html(loading);
	jQuery.post( '".$this->createUrl('statistic')."', $('#loan-search-form').serialize(),function(data, textStatus, jqXHR){
		$('#loan-result-statistic-amount').html(data.totalAmount);
		$('#loan-result-statistic-amount-average').html(data.averageAmount);
		$('#loan-result-statistic-amount-max').html(data.maxAmount);
		$('#loan-result-statistic-amount-min').html(data.minAmount);
		$('#loan-result-statistic-rate-average').html(data.averageRate);
		$('#loan-result-statistic-rate-max').html(data.maxRate);
		$('#loan-result-statistic-rate-min').html(data.minRate);
		$('#loan-result-statistic-pricing-average').html(data.averagePricing);
		$('#loan-result-statistic-pricing-max').html(data.maxPricing);
		$('#loan-result-statistic-pricing-min').html(data.minPricing);
	}, 'json' )
};

$('#loan-search-button').click(function(){
	$.fn.yiiListView.update('loan-list', {
		data: $('#loan-search-form').serialize()
	});
	reloadStatistic();
	return false;
});
reloadStatistic();
");

?>

<div class="form">
	
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'loan-search-form',
	'action'=>Yii::app()->createUrl($this->route),
	'method'=>'get',
)); ?>
	
	<!-- Bank -->
	<div class="span-5">
		<?php echo $form->labelEx($loanSearch,'bank'); ?>
		<?php echo $form->dropDownList($loanSearch,'bank',Deal::getBankArray(),array('empty'=>'')); ?>
		<?php echo $form->error($loanSearch,'bank'); ?>
	</div>
	
	<!-- Customer Name -->
	<div class="span-5">
		<?php echo $form->labelEx($loanSearch,'customerName'); ?>
		<?php echo $form->textField($loanSearch,'customerName',array('size'=>20,'maxlength'=>255)); ?>
		<?php echo $form->error($loanSearch,'customerName'); ?>
	</div>
	
	<!-- Team -->
	<?php if(Yii::app()->user->checkAccess('manager')):?>
	<div class="span-6">
		<?php echo $form->labelEx($loanSearch,'teamId'); ?>
		<?php echo $form->dropDownList($loanSearch,'teamId',Team::getArray(),array('empty'=>'')); ?>
		<?php echo $form->error($loanSearch,'teamId'); ?>
	</div>
	<?php endif;?>
	
	<!-- Advanced link -->
	<div class="span-7 last">
		<?php echo CHtml::link('Advanced Search','#',array('id'=>'advanced-search-button','class'=>'advanced-search-toggle')); ?>
	</div>
	
	<div class="clear"></div>
	
	<!-- Hidden Search Options -->
	<div id="advanced-search-options" style="display:none;">
	
	<!-- Product Name -->
	<div class="span-5">
		<?php echo $form->labelEx($loanSearch,'productName'); ?>
		<?php echo $form->textField($loanSearch,'productName',array('size'=>20,'maxlength'=>255)); ?>
		<?php echo $form->error($loanSearch,'productName'); ?>
	</div>

	<!-- Honeymoon Date Range -->
	<div class="span-6">
		<?php echo $form->labelEx($loanSearch,'honeymoonDateMin'); ?>
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
		    'attribute'=>'honeymoonDateMin',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?> -
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
		    'attribute'=>'honeymoonDateMax',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?>
	</div>
	
	<!-- Settlement Date Range -->
	<div class="span-6">
		<?php echo $form->labelEx($loanSearch,'settlementDateMin'); ?>
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
		    'attribute'=>'settlementDateMin',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?> -
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
		    'attribute'=>'settlementDateMax',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?>
	</div>
	
	<!-- Create Date Range -->
	<div class="span-6 last">
		<?php echo $form->labelEx($loanSearch,'createDateMin'); ?>
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
		    'attribute'=>'createDateMin',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?> -
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
		    'attribute'=>'createDateMax',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?>
	</div>
	<div class="clear"></div>
	
	<!-- Amount Range -->
	<div class="span-5">
		<?php echo $form->labelEx($loanSearch,'amountMin'); ?>
		<?php echo $form->textField($loanSearch,'amountMin',array('style'=>'width: 58px','maxlength'=>20)); ?> - 
		<?php echo $form->textField($loanSearch,'amountMax',array('style'=>'width: 58px','maxlength'=>20)); ?>
		<?php echo $form->error($loanSearch,'amountMin'); ?>
		<?php echo $form->error($loanSearch,'amountMax'); ?>
	</div>
	
	<!-- Rate Range -->
	<div class="span-3">
		<?php echo $form->labelEx($loanSearch,'rateMin'); ?>
		<?php echo $form->textField($loanSearch,'rateMin',array('style'=>'width: 37px','maxlength'=>20)); ?> - 
		<?php echo $form->textField($loanSearch,'rateMax',array('style'=>'width: 37px','maxlength'=>20)); ?>
		<?php echo $form->error($loanSearch,'rateMin'); ?>
		<?php echo $form->error($loanSearch,'rateMax'); ?>
	</div>
	
	<!-- Commission Paid -->
	<div class="span-3">
		<?php echo $form->labelEx($loanSearch,'commissionPaid'); ?>
		<?php echo $form->dropDownList($loanSearch,'commissionPaid',DataUtil::getOptionYesNo(null,true)); ?>
	</div>
	
	<!-- Pricing Range -->
	<div class="span-4">
		<?php echo $form->labelEx($loanSearch,'pricingMin'); ?>
		<?php echo $form->textField($loanSearch,'pricingMin',array('style'=>'width: 57px','maxlength'=>20)); ?> - 
		<?php echo $form->textField($loanSearch,'pricingMax',array('style'=>'width: 57px','maxlength'=>20)); ?>
		<?php echo $form->error($loanSearch,'pricingMin'); ?>
		<?php echo $form->error($loanSearch,'pricingMax'); ?>
	</div>
	
	<!-- Discharged & Date Range -->
	<div class="span-8 last">
		<?php echo $form->labelEx($loanSearch,'discharged'); ?>
		<?php echo $form->dropDownList($loanSearch,'discharged',DataUtil::getOptionYesNo(null,true)); ?>
		if Yes:
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
			'id'=>'discharge-date-min-picker',
		    'attribute'=>'dischargeDateMin',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?> -
		<?php 
		$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		    'model'=>$loanSearch,
			'id'=>'discharge-date-max-picker',
		    'attribute'=>'dischargeDateMax',
			'htmlOptions'=>array(
				'style'=>'width: 95px',
			),
		    ));  
		?>
	</div>
	
	<!-- If Hidden -->
	<div class="clear"></div>
	<?php if(GlobalConfiguration::isManageMode()): ?>
	<div class="span-8 last">
		<?php echo $form->labelEx($loanSearch,'hidden'); ?>
		<?php echo $form->dropDownList($loanSearch,'hidden',DataUtil::getOptionYesNo(null,true)); ?>
	</div>
	<div class="clear"></div>
	<?php endif; ?>
	</div>
	
	<!-- Three Buttons -->
	<div class="row buttons">
		<?php echo CHtml::submitButton('Search',array('id'=>'loan-search-button','name'=>'action')); ?>
		<?php echo CHtml::resetButton('Clear'); ?>
		<?php echo CHtml::submitButton('Export Result',array('id'=>'loan-export-button','name'=>'action')); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- search-form -->

<div id="loan-result-statistic">
	<div id="loan-result-statistic-title">Statistics</div>
	<div class="span-4">Total Amount ($):</div>
	<div id="loan-result-statistic-amount" class="span-3">Loading...</div>
	<div class="span-4">Average Amount ($):</div>
	<div id="loan-result-statistic-amount-average" class="span-3">Loading...</div>
	
	<div class="clear"></div>
	
	<div class="span-4">Max Amount ($):</div>
	<div id="loan-result-statistic-amount-max" class="span-3">Loading...</div>
	<div class="span-4">Min Amount ($):</div>
	<div id="loan-result-statistic-amount-min" class="span-3">Loading...</div>
	<div class="clear"></div>

	<div class="span-4">Average Rate (%):</div>
	<div id="loan-result-statistic-rate-average" class="span-3">Loading...</div>
	<div class="span-4">Max Rate (%):</div>
	<div id="loan-result-statistic-rate-max" class="span-3">Loading...</div>
	<div class="span-4">Min Rate (%):</div>
	<div id="loan-result-statistic-rate-min" class="span-3">Loading...</div>
	<div class="clear"></div>
	
	<div class="span-4">Average Pricing (%):</div>
	<div id="loan-result-statistic-pricing-average" class="span-3">Loading...</div>
	<div class="span-4">Max Pricing (%):</div>
	<div id="loan-result-statistic-pricing-max" class="span-3">Loading...</div>
	<div class="span-4">Min Pricing (%):</div>
	<div id="loan-result-statistic-pricing-min" class="span-3">Loading...</div>
	<div class="clear"></div>
</div>
<?php $this->widget('zii.widgets.CListView', array(
	'id'=>'loan-list',
	'dataProvider'=>$loanSearch->search(),
	'itemView'=>'_view',
)); ?>
